package edu.suse.service.impl;
import com.fasterxml.jackson.annotation.JsonFormat;
import edu.suse.common.base.RespResult;
import edu.suse.common.jwt.JwtUtil;
import edu.suse.common.vo.RespLoginVO;
import edu.suse.entity.SysUser;
import edu.suse.mapper.SysUserMapper;
import edu.suse.service.SysUserService;
import edu.suse.utils.MD5Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;
import java.util.UUID;

//业务逻辑处理：日志、事务、安全等
@Service // = new SysUserServiceImpl();
@Slf4j
public class SysUserServiceImpl implements SysUserService {

    // 调用Mapper层
    @Resource
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    private SysUserMapper sysUserMapper;

    @Override
    public RespResult login(String username, String password) {
        // 调用mapper层实现数据查询
        // 需求：根据用户名称查询用户信息
        SysUser sysUser = sysUserMapper.findSysUserByUsername(username);
        log.info("客户端传递的用户名称：{},明文密码为：{},",username,password);
        // 将密码加密：MD5+盐值  AES
        String newPassword = MD5Utils.encrypt(password,MD5Utils.MD5_KEY);
        log.info("客户端传递的加密后的密码为: {}",newPassword);
        /**
         * sysUser != null 证明登录用户名称在表中存在的
         * sysUser == null 证明登录用户名称在表中不存在的
         * sysUser.getPassword().equals(newPassword)  证明登录密码是一致的
         */
        if(sysUser != null && sysUser.getPassword().equals(newPassword)){//登录成功

            //登录成功后应该将登录用户信息返回回去
            RespLoginVO respLoginVO = new RespLoginVO();
            respLoginVO.setNickname(sysUser.getNickname());
            respLoginVO.setUsername(sysUser.getUsername());
            respLoginVO.setPicture(sysUser.getPicture());
//             生成token
            String token = JwtUtil.createToken(UUID.randomUUID().toString(),"gxa",sysUser.getUsername(),30*60*1000);
            respLoginVO.setAccessToken(token);
            return RespResult.success(respLoginVO);
        }else{//登录用户名称或密码有误！
            return RespResult.error("登录用户名称或密码有误!");
        }
    }

    @Override
    public RespResult findSysUserAll() {
        List<SysUser> sysUserList =  sysUserMapper.querySysUserList();
        return RespResult.success(sysUserList);
    }
    @Override
    public RespResult addSysUser(SysUser su){
        Timestamp createDate = new Timestamp(System.currentTimeMillis());
        String password = su.getPassword();
        String newPassword = MD5Utils.encrypt(password,MD5Utils.MD5_KEY);
        su.setPassword(newPassword);
        su.setUpdateDate(createDate);
        su.setCreateDate(createDate);
        SysUser sysUser= sysUserMapper.addSysUser(su);
        return RespResult.success(sysUser);
    }
    @Override
    public RespResult editSysUser(SysUser es){
        Timestamp updateDate = new Timestamp(System.currentTimeMillis());
        String password = es.getPassword();
        String newPassword = MD5Utils.encrypt(password,MD5Utils.MD5_KEY);
        es.setPassword(newPassword);
        System.out.println(password);
        es.setUpdateDate(updateDate);
        SysUser sysUser= sysUserMapper.editSysUser(es);
        return RespResult.success(sysUser);
    }
    @Override
    public RespResult findSysUserById(int id){
        SysUser sysUserId=sysUserMapper.findSysUserById(id);
        return RespResult.success(sysUserId);
    }
    @Override
    public  RespResult deleteSysUserById(int id){
        SysUser sysUserde= sysUserMapper.deleteSysUserById(id);
        return RespResult.success(sysUserde);
    }
}
